<?php
declare (strict_types=1);

namespace app\api\controller\customer;

use app\api\controller\Api;
use app\common\customer\model\CustomerSign;
use app\common\customer\validate\CustomerSignValidate;

class Sign extends Api {
    
    public function index()
    {
        $post = $this->request->post();
        if (empty($post['allocate_id'])) return error([], "参数错误");
        $result = [];

        $where = [];
        $where[] = ['allocate_id', '=', $post['allocate_id']];
        $model = new CustomerSign();
        $list = $model->where($where)->select();
        foreach($list as $row) {
            $row->append(['user']);
        }

        $result['list'] = $list;

        return success($result);
    }

    public function create()
    {
        $post = $this->request->post();
        if (empty($post['member_id'])) return error([], "参数错误");

        $validate = new CustomerSignValidate();
        if (!$validate->check($post)) {
            return error([], $validate->getError());
        }

        $user = $this->request->user;
        $data = [
            'user_id' => $user['id'],
            'member_id' => $post['member_id'],
            'allocate_id' => $post['allocate_id']
        ];

        $model = new CustomerSign();
        $result = $model->save($data);

        if ($result) {
            return success([], "签到成功");
        } else {
            return error([], "签到失败");
        }
    }
}